ABOUT THE PROGRAM
I created a class named “HelloWorld” containing a simple main function within it. The keyword class specifies that we are defining a class. The name of a public class is spelled
exactly as the name of the file (Case Sensitive). All java programs begin execution with the method named main(). main method that gets executed has the following signature : public
static void main(String args[]).Declaring this method as public means that it is accessible from outside the class so that the JVM can find it when it looks for the program to start
it.It is necessary that the method is declared with return type void (i.e. no arguments are returned from the method).
The main method contains a String argument array that can contain the command line arguments. The brackets { and } mark the beginning and ending of the class. The program contains a
line ‘System.out.println(“Hello World”);’ that tells the computer to print out on one line of text namely ‘Hello World’. The semi-colon ‘;’ ends the line of code. The double slashes
‘//’ are used for comments that can be used to describe what a source code is doing. Everything to the right of the slashes on the same line does not get compiled, as they are simply
the comments in a program.
Java Main method Declarations
Class MainExample1 {public static void main(String[] args) {}}
class MainExample2 {public static void main(String []args) {}}
class MainExample3 {public static void main(String args[]) {}}
All the 3 valid main method’s shown above accepts a single String array argument.
Java Main method Declarations
Class MainExample1 {public static void main(String[] args) {}}
class MainExample2 {public static void main(String []args) {}}
class MainExample3 {public static void main(String args[]) {}}
All the 3 valid main method’s shown above accepts a single String array argument.
Compiling and Running an Application
To compile and run the program you need the JDK distributed by Sun Microsystems. The JDK contains documentation, examples, installation instructions, class libraries and packages, and tools. Download an editor like Textpad/EditPlus to type your code. You must save your source code with a .java extension. The name of the file must be the name of the public class contained in the file.Steps for Saving, compiling and Running a Java
Step 1:Save the program With .java Extension.
Step 2:Compile the file from DOS prompt by typing javac
Step 3:Successful Compilation, results in creation of .class containing byte code
Step 4:Execute the file by typing java
Java Development Kit
The Java Developer’s Kit is distributed by Sun Microsystems. The JDK contains documentation, examples, installation instructions, class libraries and packages, and tools.Javadoc
The javadoc tool provided by Sun is used to produce documentation for an application or program,Jar Files
A jar file is used to group together related class files into a single file for more compact storage, distribution, and transmission.PATH and CLASSPATH
The following are the general programming errors, which I think every beginning java programmer would come across. Here is a solution on how to solve the problems when running on a Microsoft Windows Machine.1. ‘javac’ is not recognized as an internal or external command, operable program or batch file
How to set the PATH Variable?
Firstly the PATH variable is set so that we can compile and execute programs from any directory without having to type the full path of the command. To set the PATH of jdk on your system (Windows XP), add the full path of the jdk
a. Click Start > Right Click “My Computer” and click on “Properties”
b. Click Advanced > Environment Variables.
c. Add the location of bin folder of JDK installation for PATH in User Variables and System Variables.
A typical value for PATH is:
C:\jdk
2. Exception in thread “main” java.lang.NoClassDefFoundError: HelloWorld
If you receive this error, java cannot find your compiled byte code file, HelloWorld.class.If both your class files and source code are in the same working directory and if you try running your program from the current working directory than, your program must get executed without any problems as, java tries to find your .class file is your current directory. If your class files are present in some other directory other than that of the java files we must set the CLASSPATH pointing to the directory that contain your compiled class files. CLASSPATH can be set as follows on a Windows machine:
a. Click Start > Right Click “My Computer” and click on “Properties”
b. Click Advanced > Environment Variables.
Add the location of classes’ folder containing all your java classes in User Variables.
If there are already some entries in the CLASSPATH variable then you must add a semicolon and then add the new value . The new class path takes effect in each new command prompt window you open after setting the CLASSPATH variable.